<template>
  <div class="fontDa">
    <el-dialog :visible.sync="dialogVisible" fullscreen destroy-on-close append-to-body :show-close="showClo">
      <el-steps :active="activeName" finish-status="success" simple style="display: none">
        <el-step :title="item.label" v-for="(item,index) in tabsList"></el-step>
        <el-step title="评票汇总"></el-step>
      </el-steps>
      <!--      <el-tabs v-model="activeName" @tab-click="handleTabClick(activeName)">-->
      <!--        <el-tab-pane disabled :label="item.label" :name="item.name" v-for="(item,index) in tabsList" :key="index"></el-tab-pane>-->
      <!--      </el-tabs>-->
      <!--  选票-->
      <div v-if="mode=='df'">
        <!--测评票( 票1-2 )-->
        <ce-ping v-if="refs=='ceping'" ref="ceping" @nextTab="nextTab" @lastTab="lastTab" @toHuiZong="toHuiZong"
                 @closeDialog="closeDialog"></ce-ping>
        <!--测评票( 票3 )-->
        <ce-pingpz3 v-if="refs=='ceping3'" ref="ceping3" @nextTab="nextTab" @lastTab="lastTab" @toHuiZong="toHuiZong"
                    @closeDialog="closeDialog"></ce-pingpz3>
        <!--测评票( 票4 )-->
        <ce-pingpz4 v-if="refs=='ceping4'" ref="ceping4" @nextTab="nextTab" @lastTab="lastTab" @toHuiZong="toHuiZong"
                    @closeDialog="closeDialog"></ce-pingpz4>
        <!--测评票( 票5 )-->
        <ce-pingpz5 v-if="refs=='ceping5'" ref="ceping5" @nextTab="nextTab" @lastTab="lastTab" @toHuiZong="toHuiZong"
                    @closeDialog="closeDialog"></ce-pingpz5>
      </div>
      <!--  汇总-->
      <div v-if="mode=='hz'">
        <cpHzPz ref="cpHzPz" @openXp="openXp" @backFromPage="backFromPage" @tiJiaoDone="tiJiaoDone"></cpHzPz>
        <!--        <div class="btn-box">-->
        <!--          <el-button type="primary" @click="tiJiao()" class="nextBtn">提交测评</el-button>-->
        <!--        </div>-->
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getUnPickTicket,getUnPickTicketSm} from "@/api/ceping/jxevalvote";
import cePing from "./cpIndex";
import cePingpz3 from "./cpIndexpz3";
import cePingpz4 from "./cpIndexpz4";
import cePingpz5 from "./cpIndexpz5";
import cpHzPz from "./cpIndexHzPz";
import store from "@/store";

export default {
  name: "cpIndex.vue",
  components: {
    cePing,
    cePingpz3,
    cePingpz4,
    cePingpz5,
    cpHzPz
  },
  data() {
    return {
      dialogVisible: false,
      hdglXpxxbList: [],
      activeName: 0,
      tabsList: [],
      actity: true,
      actity3: true,
      actity4: true,
      actity5: true,
      xpCode: '',
      showClo: false,
      refs: '',
      dqwz: 0,
      mode: "df",//df或hz
      xcCode: '',
      hdId: '',
      sheetType: 1,
      allPageData: [],
      doneList: [],
      currentPage: 0,
      currentPageObj: {},
      upPageObj: {},
      operate: '',
      userId:''
    }
  },
  beforeDestroy() {
    clearInterval(this.timer);
  },
  methods: {

    //初始按会议
    initHd(hdglXpxxbList, hdId) {
      this.type = "HD";
      this.userId=this.$store.state.user.userInfo.userId
      this.formatData(hdglXpxxbList, '', hdId);
    },
    //初始按选票
    initXp(hdglXpxxbList, xpCode) {
      this.type = "XP";
      this.formatData(hdglXpxxbList, xpCode)
    },
    //整理数据
    formatData(hdglXpxxbList, xpCode, hdId) {
      this.dialogVisible = true;
      this.tabsList = [];
      this.xpCode = xpCode;
      this.hdId = hdglXpxxbList[0].hdId;
      var sheetType1 = hdglXpxxbList.find(item => item.sheetType == '1');
      var sheetType3 = hdglXpxxbList.find(item => item.sheetType == '3');
      var sheetType4 = hdglXpxxbList.find(item => item.sheetType == '4');
      var sheetType5 = hdglXpxxbList.find(item => item.sheetType == '5');
      // 第一个
      if (sheetType1) {
        this.tabsList.push({
          label: '测评票( 票1-2 )', name: 'ceping', sheetType: 1
        })
      }
      // 第三个
      if (sheetType3) {
        this.tabsList.push({
          label: '测评票( 票3 )', name: 'ceping3', sheetType: 3
        })
      }
      // 第四个
      if (sheetType4) {
        this.tabsList.push({
          label: '测评票( 票4 )', name: 'ceping4', sheetType: 4
        })
      }
      // 第五个
      if (sheetType5) {
        this.tabsList.push({
          label: '测评票( 票5 )', name: 'ceping5', sheetType: 5
        })
      }
      // this.refs = this.tabsList[this.activeName].name;
      this.sheetType = this.tabsList[this.activeName].sheetType;
      if(this.type=='HD'){
        //暂时取消第一次验证不能同一个人打票
        this.getPickTicketSm('first')
        //this.getPickTicketSm('nofirst')
      }else{
        //暂时取消第一次验证不能同一个人打票
        this.getPickTicket('first')
        //this.getPickTicket('nofirst');
      }
      // this.$nextTick(()=>{
      //   this.handleTabClick(this.refs,this.sheetType);
      // })
    },
    handleClose(done) {
      this.$confirm('您的测评还没有完成，请继续完成测评。', "提示", {
        confirmButtonText: '执行操作',
        showConfirmButton: false,
        cancelButtonText: '继续打分',
        type: 'warning'
      }).then(_ => {
        done();
      }).catch(_ => {
      });
    },
    //切换tab选择
    handleTabClick(val, ishaveLast, currentPageObj) {
      this.mode = 'df'
      var isNotLastTab = (this.tabsList.length - 1) == this.activeName ? false : true;
      this.refs = val;
      var params = {
        xpCode: this.xpCode,
        hdId: this.hdId,
        sheetType: this.sheetType,
        isNotLastTab: isNotLastTab,
        ishaveLast: ishaveLast,
        allPageData: this.allPageData,
        doneList: this.doneList,
        currentPage: this.currentPage,
        currentPageObj: currentPageObj
      }
      this.$nextTick(() => {
        if (this.type == 'HD') {
          this.$refs[this.refs].initHd(params);
        } else {
          this.$refs[this.refs].initXp(params);
        }
      })
    },

    //获取当前数据打票情况(实名)
    getPickTicketSm(inTime, upPageObj) {
      this.upPageObj = upPageObj;
      var params = {
        hdId: this.hdId,
        xpCode: this.xpCode,
        userId: this.userId
      }
      getUnPickTicketSm(params).then(res => {
        if (res.data.code == 0) {
          var refs = "", voteId = '';
          var currentXpCodeArr = res.data.data;
          this.doneList = currentXpCodeArr.filter(item => item.dpZt == 1);
          this.notdoList = currentXpCodeArr.filter(item => item.dpZt == 0);
          this.allPageData = currentXpCodeArr;
          var currentPageObj = {};
          if (this.notdoList.length == 0) {
            this.currentPage = inTime == 'first' ? 0 : this.currentPage;
            if (this.currentPage != this.allPageData.length) {
              this.currentPage = this.currentPage + 1;
              currentPageObj = this.allPageData[this.currentPage - 1]
            } else {
              //直接去汇总页
              var currentPageObj = this.allPageData[this.currentPage - 1];
              var obj = this.tabsList.find(item => currentPageObj.sheetType == item.sheetType)
              this.mode = 'hz'
              this.openHz(obj.name, '', currentPageObj.cppId);
              return false;
            }
          } else {
            var findIndex = this.allPageData.findIndex(item => item.dpZt == 0)
            if (findIndex != -1) {
              this.currentPage = findIndex + 1;
            }
            currentPageObj = this.allPageData[findIndex]
          }
          this.currentPageObj = currentPageObj;
          if (currentPageObj) {
            var obj = this.tabsList.find(item => currentPageObj.sheetType == item.sheetType)
            refs = obj.name
            var ishaveLast = this.currentPage > 1 ? true : false;
            if(inTime == 'first'){
              ishaveLast=false;
            }
            this.handleTabClick(refs, ishaveLast, currentPageObj)
          }
        }
      })
    },
    //获取当前数据打票情况
    getPickTicket(inTime, upPageObj) {
      this.upPageObj = upPageObj;
      var params = {
        hdId: this.hdId,
        xpCode: this.xpCode
      }
      getUnPickTicket(params).then(res => {
        console.log('getUnPickTicket返回值',res)
        if (res.data.code == 0) {
          var refs = "", voteId = '';
          var currentXpCodeArr = res.data.data;
          this.doneList = currentXpCodeArr.filter(item => item.dpZt == 1);
          this.notdoList = currentXpCodeArr.filter(item => item.dpZt == 0);
          this.allPageData = currentXpCodeArr;
          var currentPageObj = {};
          if (this.notdoList.length == 0) {
            this.currentPage = inTime == 'first' ? 0 : this.currentPage;
            if (this.currentPage != this.allPageData.length) {
              this.currentPage = this.currentPage + 1;
              currentPageObj = this.allPageData[this.currentPage - 1]
            } else {
              //直接去汇总页
              var currentPageObj = this.allPageData[this.currentPage - 1];
              var obj = this.tabsList.find(item => currentPageObj.sheetType == item.sheetType)
              this.mode = 'hz'
              this.openHz(obj.name, '', currentPageObj.cppId);
              return false;
            }
          } else {
            var findIndex = this.allPageData.findIndex(item => item.dpZt == 0)
            if (findIndex != -1) {
              this.currentPage = findIndex + 1;
            }
            currentPageObj = this.allPageData[findIndex]
          }
          this.currentPageObj = currentPageObj;
          if (currentPageObj) {
            var obj = this.tabsList.find(item => currentPageObj.sheetType == item.sheetType)
            refs = obj.name
            var ishaveLast = this.currentPage > 1 ? true : false;
            if(inTime == 'first'){
              ishaveLast=false;
            }
            this.handleTabClick(refs, ishaveLast, currentPageObj)
          }
        }
      })
    },
    //获取当前数据打票情况(分页也变)，现在备用，未正常使用
    getPickTicket1(inTime, upPageObj) {
      this.upPageObj = upPageObj;
      getUnPickTicket({hdId: this.hdId, xpCode: this.xpCode}).then(res => {
        if (res.data.code == 0) {
          var refs = "", voteId = '';
          var currentXpCodeArr = res.data.data;
          this.doneList = currentXpCodeArr.filter(item => item.dpZt == 1);
          this.notdoList = currentXpCodeArr.filter(item => item.dpZt != 1);
          this.allPageData = this.doneList.concat(this.notdoList);
          var currentPageObj = {};
          if (this.notdoList.length == 0) {
            this.currentPage = inTime == 'first' ? 0 : this.currentPage;
            if (this.currentPage != this.allPageData.length) {
              this.currentPage = this.currentPage + 1;
              currentPageObj = this.allPageData[this.currentPage - 1]
            } else {
              //直接去汇总页
              var currentPageObj = this.allPageData[this.currentPage - 1];
              var obj = this.tabsList.find(item => currentPageObj.sheetType == item.sheetType)
              this.mode = 'hz'
              this.openHz(obj.name, '', currentPageObj.cppId);
              return false;
            }
          } else {
            this.currentPage = this.doneList.length + 1;
            currentPageObj = this.notdoList[0]
          }
          this.currentPageObj = currentPageObj;
          if (currentPageObj) {
            var obj = this.tabsList.find(item => currentPageObj.sheetType == item.sheetType);
            refs = obj.name
            var ishaveLast = this.currentPage > 1 ? true : false
            this.handleTabClick(refs, ishaveLast, currentPageObj)
          }
        }
      })
    },
    //跳转下一个tab
    nextTab(data) {
      //跳转下一页之前先把当前页的信息赋值给this.upPageObj，当作上一页的信息
      if(this.type=='HD'){
        this.getPickTicketSm('nofirst', data)
      }else{
        this.getPickTicket('nofirst', data);
      }
      // var activeTabIndex=this.tabsList.findIndex(item=>item.name==this.activeName);
      // var lastTab=this.tabsList[activeTabIndex+1].name;
      //this.handleTabClick(lastTab);
      //根据后台的数据如果返回有数据就走返回的数据，如果没有数据，则走原来正常的顺序
      // getUnPickTicket({hdId:this.hdId}).then(res=>{
      //   if(res.data.code==0){
      //     var resData=res.data.data;
      //     if(!resData){
      //       if (this.activeName == (this.tabsList.length - 1)) {
      //         //this.$message.warning('这是最后一步!')
      //         this.activeName++;
      //         //进入汇总页
      //         this.mode = "hz";
      //         this.openHz(val);
      //         return false;
      //       };
      //       this.activeName++;
      //       this.refs = this.tabsList[this.activeName].name;
      //       this.handleTabClick(this.refs, true);
      //     }else{
      //       var obj=this.tabsList.find(item=>item.sheetType==resData.sheetType)
      //       if(obj){
      //         var param={
      //           id:resData.voteId,
      //           clickTab:obj.name
      //         }
      //         this.openXp(param)
      //       }
      //       //this.refs = this.tabsList[this.activeName].name;
      //     }
      //   }
      // })
      return false;

    },
    //跳转到上一个tab
    lastTab() {
      var obj = {};
      if (this.currentPage > 1) {
        obj = this.allPageData[this.currentPage - 2];
      } else {
        //直接去第一页
        obj = this.allPageData[0];
      }
      //直接去第一页
      if (obj) {
        var tabObj = this.tabsList.find(item => item.sheetType == obj.sheetType)
        var params = {
          id: tabObj.name == 'ceping' ? obj.voteId : obj.cppId,
          clickTab: tabObj.name,
        }
        this.openXp(params)
      }
    },
    //打开汇总页面
    openHz(fromPage, xpid, cppId) {
      if (this.mode == "hz") {
        this.$nextTick(() => {
          var params = {
            hdId: this.hdId,
            xpCode: this.xpCode,
            tabsList: this.tabsList,
            fromPage: fromPage,
            xpid: xpid,
            cppId: cppId,
            type: this.type
          }
          this.$refs.cpHzPz.initHzAll(params)
          //this.$refs.cpHzPz.initHzAll(this.hdId,this.xpCode,this.tabsList,fromPage,xpid)
        })
      }
    },
    //打开选票页面
    openXp(data) {
      console.log('腹肌页面的openxp',data)
      this.mode = "df";
      this.refs = data.clickTab;
      this.activeName = this.tabsList.findIndex(item => item.name == this.refs);
      //上一页页面
      this.upPageObj = data.upPageObj;
      var findOpenPageIndex = 0;
      if (data.clickTab == 'ceping') {
        if(data.cppId){
          findOpenPageIndex = this.allPageData.findIndex(item => item.cppId == data.cppId);
        }else{
          findOpenPageIndex = this.allPageData.findIndex(item => item.voteId == data.id);
        }
        if (findOpenPageIndex == -1) {
          findOpenPageIndex = this.currentPage - 1;
        }
      } else {
        findOpenPageIndex = this.allPageData.findIndex(item => item.cppId == data.id)
      }

      var findOpenPageObj = this.allPageData[findOpenPageIndex];
      this.currentPage = findOpenPageIndex + 1;
      this.currentPageObj = findOpenPageObj;
      this.$nextTick(() => {
        var param = {
          id: data.id,
          hdId: this.hdId,
          xcCode: this.xpCode,
          allPageData: this.allPageData,
          doneList: this.doneList,
          currentPage: this.currentPage,
          currentPageObj: this.currentPageObj
        }
        if (this.type == 'HD') {
          this.$refs[this.refs].openHzToHd(param);
        } else {
          this.$refs[this.refs].openHzToXp(param);
        }
      })

    },
    //返回到汇总页面
    toHuiZong(data) {
      this.activeName = this.tabsList.length;
      this.mode = "hz";
      this.openHz(data.clickTab, data.xpid, data.cppId);
    },
    //从汇总页返回到原来的测评页面
    backFromPage(data) {
      var param = {
        id: data.toPage == 'ceping' ? data.tpXpId : data.cppId,
        //id:data.cppId,
        clickTab: data.toPage
      }
      this.openXp(param)
    },
    //已经完成提交测评
    tiJiaoDone() {
      this.dialogVisible = false;
      this.cacel()
    },
    //选票信息不存在等情况出现时关闭窗口
    closeDialog() {
      console.log('closeDialog')
      this.dialogVisible = false;
      this.cacel(true)
    },
    //提交loading
    openLoading() {
      const loading = this.$loading({
        lock: true,
        text: "正在提交，请稍等...",
        spinner: "el-icon-loading",
        background: "rgba(255, 255, 255, 0.7)",
      });
    },
    cacel(isOnlyCloseDialog) {
      try {
        //退出全屏
        function exitFullscreen() {
          if (document.exitFullscreen) {
            document.exitFullscreen();
          } else if (document.msExitFullscreen) {
            document.msExitFullscreen();
          } else if (document.mozCancelFullScreen) {
            document.mozCancelFullScreen();
          } else if (document.webkitExitFullscreen) {
            document.webkitExitFullscreen();
          }
        }
        exitFullscreen();
      } catch (e) {
      }
      if(!isOnlyCloseDialog){
        console.log('刷新页面')
        this.$emit('next');
      }
    },
  }
}
</script>

<style scoped>
.btn-box {
  text-align: center;
  position: absolute;
  bottom: 0px;
  width: 100%;
  background: #fff;
  padding: 10px 0;
}
</style>
